package net.begincode.common.exception;

import com.fasterxml.jackson.annotation.JsonIgnore;

import java.io.Serializable;

public class Status implements Serializable {

    public static final int ERROR = -1024;
    public static final int SUCCESS = 0;

    private final int statusCode;
    private final String reason;

    private Status(int statusCode, String reason) {
        this.statusCode = statusCode;
        this.reason = reason;
    }

    public static Status create(int statusCode, String reason) {
        return new Status(statusCode, reason);
    }

    public static Status success() {
        return create(SUCCESS, null);
    }

    public static Status error(String reason) {
        return create(ERROR, reason);
    }

    public static Status error(int statusCode, String reason) {
        if (statusCode >= SUCCESS) {
            throw new IllegalArgumentException("statusCode must less than 0");
        }
        return create(statusCode, reason);
    }

    @JsonIgnore
    public boolean isSuccess() {
        return statusCode >= SUCCESS;
    }

    public int getStatusCode() {
        return statusCode;
    }

    public String getReason() {
        return reason;
    }

    @Override
    public String toString() {
        return new StringBuilder()
                .append(reason)
                .append('(')
                .append(statusCode)
                .append(')')
                .toString();
    }
}
